import { Game } from './Game';
import AudioManager from '../../Common/AudioManager';
import GameCtr from '../../Controller/GameCtr';
import GameData from '../../Common/GameData';
import GameDataInstance from '../../Common/GameDataInstance';
import SoundManager from '../../Common/SoundManager';
import { PlayerStorage } from '../../Common/PlayerStorageManager';
const { ccclass, property } = cc._decorator;

@ccclass
export default class GameSelectManager extends cc.Component {

    @property([cc.SpriteFrame])
    arrSprJack: cc.SpriteFrame[] = [];

    moveNd: cc.Node = null;
    gameRoom: Game = null;
    enable = true;
    originPos = null;
    canMove = false;
    isDraged = false;
    isChoosed = false;
    selectCubeId = [];
    isTouch = false;
    isfly = false;

    //倒计时
    @property(cc.RichText)
    time:cc.RichText = null

    stop:boolean = false

    onLoad() {
        this.registerTouchEvent();
        if (!this.originPos) {
            this.originPos = this.node.getPosition();
        }
    }

    setSpr() {
        for (let index = 0; index < 6; index++) {
            let element = this.node.children[index];
            element.getComponent(cc.Sprite).spriteFrame = null;
        }
        for (let index = 0; index < this.selectCubeId.length; index++) {
            let id = this.selectCubeId[index];
            let element = this.node.children[id];

            element.getComponent(cc.Sprite).spriteFrame = this.arrSprJack[GameData.level % 10];
        }
    }



    setisfrist(frist) {
        // this.isfrist = frist;
    }

    start() {

        this.daojishi()
    }

    setSelectCubeId(arrid) {
        this.selectCubeId = arrid;
    }

    setisTouch(bTouch) {
        this.isTouch = bTouch;
    }

    public registerTouchEvent() {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this, true);
        // this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this, true);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this, true);
    }

    flyNeedShow(ishow) {
        if (ishow) {
            for (let index = 0; index < this.selectCubeId.length; index++) {
                let id = this.selectCubeId[index];
                let element = this.node.children[id];
                element.getComponent(cc.Sprite).spriteFrame = this.arrSprJack[GameData.level % 10];
            }
        }
        else {
            for (let index = 0; index < 6; index++) {
                let element = this.node.children[index];
                element.getComponent(cc.Sprite).spriteFrame = null;
            }
        }
    }

    public moveTopos(poscubeid) {

        
      
        if (poscubeid > -1) {
            if (this.isfly == false) {
                this.isfly = true;
                this.moveNd = cc.instantiate(this.node);
                this.moveNd.active = true;
                this.flyNeedShow(false);
                this.moveNd.getComponent(cc.Sprite).spriteFrame = null;
                this.moveNd.getChildByName("guangquan").active = false;//.spriteFrame = null;
                this.moveNd.setPosition(this.node.getPosition());
                if (!this.moveNd.parent) {
                    this.moveNd.parent = this.node.parent;
                }
                let pos = this.gameRoom.GameCubeManager.children[poscubeid].getPosition();
                this.moveNd.runAction(cc.sequence(
                    cc.moveTo(0.2, pos),
                    cc.callFunc(function () {
                       
                        console.log('啊哈哈哈哈')
                        console.log("poscubeid", poscubeid);
                        let betSuc = this.gameRoom.FkxxlbetJudge(poscubeid, this.selectCubeId);
                        if (betSuc) {
                            console.log('成功合并')
                            if(GameData.level>=0&&GameData.level<6)
                            {
                               console.log('未成功合并也走这里了？')
                                GameDataInstance.Instance.tomecount = 10 
                            }

                            if(GameData.level>=6&&GameData.level<11)
                            {
                                GameDataInstance.Instance.tomecount = 9
                            }

                            if(GameData.level>=11&&GameData.level<16)
                            {
                                GameDataInstance.Instance.tomecount = 8
                            }
                            if(GameData.level>=16&&GameData.level<21)
                            {
                                GameDataInstance.Instance.tomecount = 7
                            }
                            if(GameData.level>=21&&GameData.level<26)
                            {
                                GameDataInstance.Instance.tomecount = 6
                            }

                           if(GameData.level>=26)
                           {
                            GameDataInstance.Instance.tomecount = 5
                           }

                           
                            this.moveNd.active = false;
                            this.gameRoom.GameCubeManager.removeChild(this.moveNd);

                            this.isfly = false;
                            this.flyNeedShow(true);
                            this.gameRoom.refeshSelectCubes(this.gameRoom.nextJackcubeId);
                        } else {
                          
                            console.log('未成功合并')
                            this.GotoOrginpos();
                        }
                    }.bind(this))
                ));
            }
        }
    }

    /** 设置当前游戏房间 */
    public setGameRoom(room) {
        this.gameRoom = room;
    }

    

    //倒计时
    daojishi()
    {
        let self = this
        this.schedule(function() {

            if(GameDataInstance.Instance.tomecount==1)
            {
                SoundManager.Instance.SetStorage(PlayerStorage.MusicOpen,false)
                //GameCtr.gameOver
                cc.director.loadScene('End')
            }
            GameDataInstance.Instance.tomecount--
            // 这里的 this 指向 component
            self.time.getComponent(cc.RichText).string = GameDataInstance.Instance.tomecount.toString()
        }, 1);
    }



    private touchStart(touch) {
        if (!this.originPos) {
            this.originPos = this.node.getPosition();
        }
        this.moveNd = cc.instantiate(this.node);
        this.moveNd.active = false;
         SoundManager.Instance.PlayEffect("up");
    }


    //放上去的时候
    private betJudge() {
        let canvas = cc.director.getScene().getChildByName("Canvas");
        if (!this.moveNd.parent) {
            return;
        }
        let wPos = this.moveNd.convertToWorldSpaceAR(this.moveNd.children[0].getPosition());
        console.log("betJudge", wPos);
        let betSuc = false;
        if (wPos) {
            betSuc = this.gameRoom.FkxxlbetJudge(wPos, this.selectCubeId);
        }
        if (betSuc) {
            this.moveNd.active = false;
            delete this.moveNd;
            this.isDraged = false;
            this.gameRoom.removeRing(this.selectCubeId);
        } else {
            this.GotoOrginpos();
        }
    }

    GotoOrginpos() {
        this.isfly = true;
        let canvas = cc.director.getScene().getChildByName("Canvas");
        if (this.originPos) {
            let oPos = canvas.convertToNodeSpaceAR(this.node.parent.convertToWorldSpaceAR(this.originPos));
            this.moveNd.runAction(cc.sequence(
                cc.moveTo(0.5, oPos),
                cc.callFunc(function () {
                    this.moveNd.active = false;
                    this.gameRoom.GameCubeManager.removeChild(this.moveNd);
                    this.isfly = false;
                    this.flyNeedShow(true);
                }.bind(this))
            ));
        }
    }
}
